<?php

/**
 * article actions.
 *
 * @package    wills
 * @subpackage article
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class articleActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	$categorys = Doctrine::getTable('ArticleCategory');
	//艺海动态
	$this->latestid=$latestid = $categorys->findOneByName("艺海动态")->getId();
	$this->latest = Doctrine::getTable('Article')->getArticleByCategory($latestid,10);

    //拍卖新闻
	$this->newsid=$newsid = $categorys->findOneByName("拍卖新闻")->getId();
	$this->news = Doctrine::getTable('Article')->getArticleByCategory($newsid,10);


	//拍卖知识
	$this->loreid=$loreid = $categorys->findOneByName("拍卖知识")->getId();
	$this->lores = Doctrine::getTable('Article')->getArticleByCategory($loreid,10);

	//拍卖专题
	$this->specials = Doctrine::getTable('Special')->createQuery('s')->limit(10)->execute();
	
	//首页广告
    $adtype = Doctrine::getTable('Adtype')
		->findOneByName("页内广告");
	$adtypeid=$adtype->getId();
	
	$this->ad =Doctrine_Query::create()
				 ->from('Ad')
				 ->Where('adtype_id = ?', $adtypeid)
				 ->addWhere('is_valid = ?',true)
				 ->fetchOne();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->article = Doctrine::getTable('Article')->find(array($request->getParameter('id')));
	
	if($tag=$this->article->getTag())
	  {
		$this->relatedarticles = Doctrine::getTable('Article')
      ->createQuery('a')
      ->addWhere('tag LIKE ?', '%'.$tag.'%')
      ->addWhere('id != ?',$this->article->getId())
      ->limit(5)
      ->execute();
	  }

	$this->category = Doctrine::getTable('ArticleCategory')->findOneById($this->article->getCategoryId());
    
	//拍卖专题
	$this->specials = Doctrine::getTable('Special')->createQuery('s')->limit(10)->execute();

  //首页广告
    $adtype = Doctrine::getTable('Adtype')
		->findOneByName("页内广告");
	$adtypeid=$adtype->getId();
	
	$this->ad =Doctrine_Query::create()
				 ->from('Ad')
				 ->Where('adtype_id = ?', $adtypeid)
				 ->addWhere('is_valid = ?',true)
				 ->fetchOne();
    $this->forward404Unless($this->article);
  }

  public function executeCategory(sfWebRequest $request)
  {
	$this->catid=$catid=$request->getParameter('catid');
	$this->category = Doctrine::getTable('ArticleCategory')->findOneById($catid);//获得当前类别名称
	
	$this->pager = new sfDoctrinePager(
      'Article',
      sfConfig::get('app_per_page_articlecategory')
    );
    $this->pager->setQuery(Doctrine::getTable('Article')
		->createQuery('a')
		->addwhere('category_id = ?',$catid)	
		);
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();
    $this->articles = $this->pager->getResults();

	//拍卖专题
	$this->specials = Doctrine::getTable('Special')->createQuery('s')->limit(10)->execute();
	
	//广告
    $adtype = Doctrine::getTable('Adtype')
		->findOneByName("页内广告");
	$adtypeid=$adtype->getId();
	
	$this->ad =Doctrine_Query::create()
				 ->from('Ad')
				 ->Where('adtype_id = ?', $adtypeid)
				 ->addWhere('is_valid = ?',true)
				 ->fetchOne();

  }

// search start
 public function executeSearch(sfWebRequest $request)
 {
	 $this->query=$query = $request->getParameter('query');
	//搜索新闻
	 $this->newsresults =Doctrine_Query::create()
		->from('Article')
		->addWhere('title LIKE ?', '%'.$query.'%')
		->execute();

	//搜索专题
	 $this->topicresults =Doctrine_Query::create()
		->from('Special')
		->addWhere('name LIKE ?', '%'.$query.'%')
		->execute();  
	//拍卖专题
	$this->specials = Doctrine::getTable('Special')->createQuery('s')->limit(10)->execute();
 }	 
// search end

}
